#### TABLES H.2-H.7: FULL MODEL RESULTS WITH COVARIATES
#### Complete regression tables for all studies

rm(list = ls())
source("./2_code/00_setup.R")

#### HELPER FUNCTION ####

extract_models <- function(model_list) {
  output_list <- list()
  for (model in model_list) {
    extracted_model <- extract(model,
                               include.rsquared = FALSE,
                               include.adjrs = FALSE,
                               include.rmse = FALSE,
                               include.ci = FALSE,
                               include.nobs = TRUE)
    output_list <- c(output_list, list(extracted_model))
  }
  return(output_list)
}


#### STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)
data$worked <- as.factor(data$worked)

# Exclusion outcomes
lin_entry_economic <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                               edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_entry_humanitarian <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                   edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                   edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                       edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])


#### STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)
data2$worked <- as.factor(data2$worked)

# Exclusion outcomes
lin_entry_economic2 <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_entry_humanitarian2 <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                    edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic2 <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                    edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian2 <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                        edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 1 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)
data3$employed <- as.factor(data3$employed)

# Exclusion outcomes
lin_entry_economic3 <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_entry_humanitarian3 <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                    edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic3 <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                    edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian3 <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                        edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Behavioral outcomes
lin_behavior_economic3 <- lm_lin(quasi_behavior ~ treat_economic, covariates = ~
                                   edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_behavior_humanitarian3 <- lm_lin(quasi_behavior ~ treat_humanitarian, covariates = ~
                                       edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 2 ####

data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)

# Exclusion outcomes
lin_entry_economic3b <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                 edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_entry_humanitarian3b <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                     edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic3b <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                     edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian3b <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                         edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])

# Behavioral outcomes
lin_behavior_economic3b <- lm_lin(quasi_behavior ~ treat_economic, covariates = ~
                                    edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_behavior_humanitarian3b <- lm_lin(quasi_behavior ~ treat_humanitarian, covariates = ~
                                        edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])


#### EXTRACT MODEL INFO ####

output_list1 <- extract_models(list(lin_entry_economic, lin_entry_humanitarian, 
                                    lin_prejudice_economic, lin_prejudice_humanitarian))

output_list2 <- extract_models(list(lin_entry_economic2, lin_entry_humanitarian2, 
                                    lin_prejudice_economic2, lin_prejudice_humanitarian2))

output_list3_w1_econ <- extract_models(list(lin_entry_economic3, lin_prejudice_economic3, 
                                            lin_behavior_economic3))

output_list3_w1_human <- extract_models(list(lin_entry_humanitarian3, lin_prejudice_humanitarian3, 
                                             lin_behavior_humanitarian3))

output_list3_w2_econ <- extract_models(list(lin_entry_economic3b, lin_prejudice_economic3b, 
                                            lin_behavior_economic3b))

output_list3_w2_human <- extract_models(list(lin_entry_humanitarian3b, lin_prejudice_humanitarian3b, 
                                             lin_behavior_humanitarian3b))


#### COEFFICIENT LABELS ####

# Studies 1 & 2
model_names_1_2 <- c("Open Mig.", "Open Mig.", "Pos. Affect", "Pos. Affect")

coeff_names_1_2 <- list(
  "treat_economic" = "Economic Treatment",
  "treat_humanitarian" = "Humanitarian Treatment",
  "edad_c" = "Age",
  "workedPrefiero no decir_c" = "Working NT",
  "workedSí_c" = "Working",
  "male_c" = "Male",
  "treat_economic:edad_c" = "Economic Treatment x Age",
  "treat_economic:workedPrefiero no decir_c" = "Economic Treatment x Working NT",
  "treat_economic:workedSí_c" = "Economic Treatment x Working",
  "treat_economic:male_c" = "Economic Treatment x Male",
  "treat_humanitarian:edad_c" = "Humanitarian Treatment x Age",
  "treat_humanitarian:workedPrefiero no decir_c" = "Humanitarian Treatment x Working NT",
  "treat_humanitarian:workedSí_c" = "Humanitarian Treatment x Working",
  "treat_humanitarian:male_c" = "Humanitarian Treatment x Male",
  "(Intercept)" = "Intercept"
)

# Study 3
model_names_3 <- c("Open Mig.", "Pos. Affect", "Behavior")

coeff_names_3 <- list(
  "treat_economic" = "Economic Treatment",
  "treat_humanitarian" = "Humanitarian Treatment",
  "edad_c" = "Age",
  "employedEstoy incapacitado permanentemente para trabajar_c" = "Disability",
  "employedEstoy jubilado o pensionado_c" = "Retired",
  "employedMe dedico a los quehaceres de mi hogar_c" = "Domestic",
  "employedNo estoy trabajando en este momento pero tengo trabajo_c" = "Looking",
  "employedNo trabajo y no estoy buscando trabajo_c" = "Not Looking",
  "employedSoy estudiante_c" = "Student",
  "employedTrabajando_c" = "Working",
  "male_c" = "Male",
  "treat_economic:edad_c" = "Economic Treatment x Age",
  "treat_economic:employedEstoy incapacitado permanentemente para trabajar_c" = "Economic Treatment x Disability",
  "treat_economic:employedEstoy jubilado o pensionado_c" = "Economic Treatment x Retired",
  "treat_economic:employedMe dedico a los quehaceres de mi hogar_c" = "Economic Treatment x Domestic",
  "treat_economic:employedNo estoy trabajando en este momento pero tengo trabajo_c" = "Economic Treatment x Looking",
  "treat_economic:employedNo trabajo y no estoy buscando trabajo_c" = "Economic Treatment x Not Looking",
  "treat_economic:employedSoy estudiante_c" = "Economic Treatment x Student",
  "treat_economic:employedTrabajando_c" = "Economic Treatment x Working",
  "treat_economic:male_c" = "Economic Treatment x Male",
  "treat_humanitarian:edad_c" = "Humanitarian Treatment x Age",
  "treat_humanitarian:employedEstoy incapacitado permanentemente para trabajar_c" = "Humanitarian Treatment x Disability",
  "treat_humanitarian:employedEstoy jubilado o pensionado_c" = "Humanitarian Treatment x Retired",
  "treat_humanitarian:employedMe dedico a los quehaceres de mi hogar_c" = "Humanitarian Treatment x Domestic",
  "treat_humanitarian:employedNo estoy trabajando en este momento pero tengo trabajo_c" = "Humanitarian Treatment x Looking",
  "treat_humanitarian:employedNo trabajo y no estoy buscando trabajo_c" = "Humanitarian Treatment x Not Looking",
  "treat_humanitarian:employedSoy estudiante_c" = "Humanitarian Treatment x Student",
  "treat_humanitarian:employedTrabajando_c" = "Humanitarian Treatment x Working",
  "treat_humanitarian:male_c" = "Humanitarian Treatment x Male",
  "(Intercept)" = "Intercept"
)


#### EXPORT TO LATEX ####

# Table H.2: Study 1
latex_output <- texreg(
  output_list1,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names_1_2,
  caption = "Full Model Results of Study 1",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H2.tex"))

# Table H.3: Study 2
latex_output <- texreg(
  output_list2,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names_1_2,
  caption = "Full Model Results of Study 2",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H3.tex"))

# Table H.4: Study 3 Economic - Wave 1
latex_output <- texreg(
  output_list3_w1_econ,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Economic Treatment) – Wave 1",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H4.tex"))

# Table H.5: Study 3 Humanitarian - Wave 1
latex_output <- texreg(
  output_list3_w1_human,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Humanitarian Treatment) – Wave 1",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H5.tex"))

# Table H.6: Study 3 Economic - Wave 2
latex_output <- texreg(
  output_list3_w2_econ,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Economic Treatment) – Wave 2",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H6.tex"))

# Table H.7: Study 3 Humanitarian - Wave 2
latex_output <- texreg(
  output_list3_w2_human,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Humanitarian Treatment) – Wave 2",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H7.tex"))

